package com.example.project.activity.wode;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.example.project.MyApplication;
import com.example.project.R;
import com.example.project.activity.tubiao.MonthChartActivity;
import com.example.project.dialog.SelectTimeDialog;
import com.example.project.utils.DateUtil;
import com.example.project.utils.MonPickerDialog;
import com.example.project.utils.YearPickerDialog;
import com.example.project.volley.ConstantUtil;
import com.example.project.volley.Params;
import com.example.project.volley.VolleyInterface;
import com.example.project.volley.VolleyRequest;

import org.json.JSONException;

import java.util.Calendar;

public class ExportActivity extends AppCompatActivity implements View.OnClickListener{
    TextView tvStart,tvEnd;//开始时间，结束时间
    int year,month,mainId; //年，月，日
    ImageView ivBack;
    String years,months,mainIds;
    Calendar calendar; //日历控件
    int selectTime = 1; //查询的  ，时间(年：0，月：1)
    RelativeLayout rlMonth,rlYear;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_export);
        initView();
        initTime();
    }

    private void initView() {
        ivBack=findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        rlMonth=findViewById(R.id.rl_month);
        rlMonth.setOnClickListener(this);
        rlYear=findViewById(R.id.rl_year);
        rlYear.setOnClickListener(this);
        tvStart=findViewById(R.id.tv_start);
        tvStart.setOnClickListener(this);
        tvEnd=findViewById(R.id.tv_end);
        tvEnd.setOnClickListener(this);
        mainId= MyApplication.getMainId(); //获取用户信息
    }

    /** 获取时间的方法*/
    private void initTime() {
        calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH)+1;
        years=String.valueOf( year );
        months=String.valueOf( month );
        mainIds=String.valueOf( mainId );
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.rl_year:
                selectYearTime();
                break;
            case R.id.rl_month:
                selectMonthTime();
                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }

    //显示年-月份框
    private void selectMonthTime() {
        new MonPickerDialog(ExportActivity.this, AlertDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                selectTime = 1;
                calendar.set(Calendar.YEAR, year);
                calendar.set(Calendar.MONTH, month);
                tvEnd.setText(DateUtil.clanderTodatetime(calendar, "yyyy-MM"));
                years=String.valueOf( year );
                month=month+1;
                months=String.valueOf( month );
                String mainIds=String.valueOf( mainId );
                volleyExport(years,months, "0",mainIds);
            }
        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE)).show();
    }

    //显示年份框
    private void selectYearTime() {

        new YearPickerDialog(ExportActivity.this, AlertDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                selectTime = 0;
                calendar.set(Calendar.YEAR, year);
                tvStart.setText(DateUtil.clanderTodatetime(calendar, "yyyy"));
                years=String.valueOf( year );
                mainIds=String.valueOf( mainId );
                volleyExport(years,"", "0",mainIds);
            }
        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE)).show();
    }



    public void volleyExport(String years,String  months, String kinds, String mainIds) {
        VolleyRequest.RequesetPost(ConstantUtil.EXPORT, "List", Params.MonthList(years, months, kinds, mainIds), new VolleyInterface(this) {

            @Override
            public void onMySuccess(String result) throws JSONException {
                Toast.makeText(ExportActivity.this,"导出成功",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onMyError(VolleyError error) {
                Toast.makeText(ExportActivity.this,"导出失败",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onImageSuccess(Bitmap bitmap) {

            }
        });

    }
}